﻿package mortal.game.control
{
    import com.gengine.core.frame.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.view.hangCount.*;
    import mortal.game.view.hangCount.data.*;
    import mortal.game.view.hangCount.util.*;
    import mortal.mvc.core.*;

    public class HangStatisticsController extends Controller
    {
        private var _timer:SecTimer;
        private var _isInCountDown:Boolean;
        private var _isInHangUp:Boolean;
        private var _isAutoFighting:Boolean;
        private var _totalGainExp:int;
        private var _itemArr:Array;
        private var _startTime:Number;

        public function HangStatisticsController()
        {
            this._itemArr = [];
            return;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.HangUpGetExp, this.onHangUpGetExp);
            NetDispatcher.addCmdListener(ServerCommand.HangUpGetItem, this.onHangUpGetItem);
            NetDispatcher.addCmdListener(ServerCommand.Copy_EnterCopy, this.onEnterCopy);
            NetDispatcher.addCmdListener(ServerCommand.Copy_EnterCopy, this.onLeaveCopy);
            Dispatcher.addEventListener(EventName.AutoFightAIStatusChange, this.hangUpStatusChangeHandler);
            return;
        }// end function

        private function onStartHangUp() : void
        {
            this._isInHangUp = true;
            if (this._timer == null)
            {
                this._timer = new SecTimer(1, HangUpConst.WaitingTime);
            }
            else
            {
                this._timer.reset();
                this._timer.repeatCount = HangUpConst.WaitingTime;
            }
            this._timer.start();
            this._timer.addListener(TimerType.COMPLETE, this.onCountDownEnd);
            this._isInCountDown = true;
            this._startTime = ClockManager.instance.nowDate.time;
            return;
        }// end function

        private function onCountDownEnd(param1:SecTimer) : void
        {
            var _loc_2:int = 0;
            this._timer.stop();
            this._isInCountDown = false;
            if (this._itemArr.length > 0)
            {
                HangCountWin.instance.show();
                _loc_2 = (ClockManager.instance.nowDate.time - this._startTime) / 1000;
                HangCountWin.instance.updateTotalTime(_loc_2);
            }
            return;
        }// end function

        public function onEndHangUp() : void
        {
            this._isInHangUp = false;
            if (this._isInCountDown)
            {
                this._timer.stop();
                this._isInCountDown = false;
            }
            if (HangCountWin.hasInstance() && !HangCountWin.instance.isHide)
            {
                HangCountWin.instance.hide();
            }
            this._totalGainExp = 0;
            this._itemArr.length = 0;
            this._startTime = 0;
            return;
        }// end function

        private function onHangUpGetExp(param1:Object) : void
        {
            var _loc_2:* = param1 as int;
            if (this._isInHangUp)
            {
                this._totalGainExp = this._totalGainExp + _loc_2;
                if (HangCountWin.hasInstance() && !HangCountWin.instance.isHide)
                {
                    HangCountWin.instance.updateGainExp();
                }
            }
            return;
        }// end function

        private function onHangUpGetItem(param1:Object) : void
        {
            var _loc_2:ItemData = null;
            var _loc_3:HangUpItemData = null;
            var _loc_4:HangUpItemData = null;
            var _loc_5:int = 0;
            if (this._isInHangUp)
            {
                _loc_2 = param1 as ItemData;
                _loc_3 = this.getDropItem(_loc_2);
                if (_loc_3 != null)
                {
                    (_loc_3.amount + 1);
                }
                else
                {
                    _loc_4 = new HangUpItemData();
                    _loc_4.itemData = _loc_2;
                    this._itemArr.push(_loc_4);
                }
                if (_loc_2 && !this._isInCountDown && HangCountWin.instance.isHide)
                {
                    HangCountWin.instance.show();
                    HangCountWin.instance.updateItemList();
                    _loc_5 = (ClockManager.instance.nowDate.time - this._startTime) / 1000;
                    HangCountWin.instance.updateTotalTime(_loc_5);
                }
                else if (HangCountWin.hasInstance() && !HangCountWin.instance.isHide)
                {
                    HangCountWin.instance.updateItemList();
                }
            }
            return;
        }// end function

        private function getDropItem(param1:ItemData) : HangUpItemData
        {
            var _loc_2:HangUpItemData = null;
            for each (_loc_2 in this._itemArr)
            {
                
                if (_loc_2.itemData.itemCode == param1.itemCode)
                {
                    return _loc_2;
                }
            }
            return null;
        }// end function

        private function hangUpStatusChangeHandler(event:DataEvent) : void
        {
            this._isAutoFighting = Boolean(event.data);
            if (this._isAutoFighting && cache.role.entityInfo.level >= 52 && (MapFileUtil.isInCrossAutofightMap || MapFileUtil.isInLocalShowAutofightWinMap) && !this._isInHangUp)
            {
                this.onStartHangUp();
            }
            else if (!this._isAutoFighting && this._isInHangUp)
            {
                this.onEndHangUp();
            }
            return;
        }// end function

        private function onEnterCopy(param1:Object) : void
        {
            if (this._isAutoFighting && GameMapUtil.curMapState.isCrossMap)
            {
                this.onStartHangUp();
            }
            return;
        }// end function

        private function onLeaveCopy(param1:Object) : void
        {
            if (this._isInHangUp && !GameMapUtil.curMapState.isCrossMap)
            {
                this.onEndHangUp();
            }
            return;
        }// end function

        public function get totalGainExp() : int
        {
            return this._totalGainExp;
        }// end function

        public function get itemArr() : Array
        {
            return this._itemArr;
        }// end function

    }
}
